package com.dm.lweb.controller;

import com.dm.lweb.common.FileUtil;
import com.dm.lweb.common.ImageToBase;
import com.dm.lweb.common.OkHttpUtils;
import com.dm.lweb.pojo.AssessExcel;
import com.dm.lweb.pojo.AssessExcel2;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "goLogis")
public class GoController {
    private static final Logger logger = LoggerFactory.getLogger(GoController.class);

    private static String BACKURL = "http://localhost:8090";
//    private static String BACKURL = "http://192.168.99.19:8080/logistics";
//    private static String BACKURL = "http://222.222.33.34:6443/logistics";

    @Autowired
    private ImageToBase imageToBase;

    @Autowired
    private OkHttpUtils okHttpUtils;

    @Autowired
    private FileUtil fileUtil;

    /*####################################################################################*/
    /*##############################   登陆管理    #######################################*/
    /*####################################################################################*/

    @RequestMapping(value="/login",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> goLogin(@RequestBody Map<String,Object> param, HttpServletRequest request){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/login",headersParams,jsonObject.toString(),new Object());
        JSONObject resultJson = JSONObject.fromObject(statusMap.get("data"));
        Map json = (Map) JSONObject.fromObject(resultJson);
        HttpSession session = request.getSession();
        if(json.size()>1){
            String username = json.get("username").toString();
            String store = json.get("store").toString();
            String userno = json.get("userno").toString();
            session.setAttribute("userno",userno);
            session.setAttribute("username",username);
            session.setAttribute("store",store);
            session.setMaxInactiveInterval(60*60);
        }
        return json;
    }

    @RequestMapping(value="/initPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initPage(HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        String Store = session.getAttribute("store")!=null?session.getAttribute("store").toString():null;
        String userno = session.getAttribute("userno")!=null?session.getAttribute("userno").toString():null;
        Map<String,Object> tMap = new HashMap<>();
        tMap.put("username",username);
        tMap.put("store",Store);
        tMap.put("userno",userno);
        JSONObject js = new JSONObject();
        if(username==null||Store==null){
            tMap.put("status","-1");
            js = JSONObject.fromObject(tMap);
            return js.toString();
        }else{
            tMap.put("status","1");
        }
        JSONObject jsonObject = JSONObject.fromObject(tMap);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/initPage",headersParams,jsonObject.toString(),new Object());
        statusMap.put("status","1");
        statusMap.put("store",Store);
        statusMap.put("username",username);
        JSONObject resultJson = JSONObject.fromObject(statusMap);
        return resultJson.toString();
    }



    /*####################################################################################*/
    /*##############################   资产管理    #######################################*/
    /*####################################################################################*/
    /**
         * @Description  查询所有资产基本信息
         * @Author zhangxue
         * @Date 10:50 2018/5/15
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllBasicEqu",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllBasicEqu(@RequestParam Map<String,Object> param, HttpServletRequest request){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllBasicEqu",headersParams,jsonObject.toString(),new Object());
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description  查询所有资产基本信息
     * @Author zhangxue
     * @Date 10:50 2018/5/15
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryAllBasicEqu2",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllBasicEqu2(@RequestParam Map<String,Object> param, HttpServletRequest request){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllBasicEqu2",headersParams,jsonObject.toString(),new Object());
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description  查询所有资产基本信息
     * @Author zhangxue
     * @Date 10:50 2018/5/15
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryAllBasicEqu3",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllBasicEqu3(@RequestParam Map<String,Object> param, HttpServletRequest request){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllBasicEqu3",headersParams,jsonObject.toString(),new Object());
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }



    /**
     * @Description 通过资产编号查看是否存在相同资产编号的资产
     * @Author zhangxue
     * @Date 9:54 2018/5/9
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryBasicEquByEquNo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryBasicEquByEquNo(@RequestBody Map<String,Object> param) {
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/queryBasicEquByEquNo",headersParams,jsonObject.toString(),new Object());
        JSONObject resultJson = JSONObject.fromObject(statusMap.get("data"));
        return resultJson.toString();
    }

    /**
     * @Description 保存新增资产信息
     * @Author zhangxue
     * @Date 9:54 2018/5/9
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/saveBasicEqu",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveBasicEqu(@RequestBody Map<String,Object> param) {
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/saveBasicEqu",headersParams,jsonObject.toString(),new Object());
        JSONObject resultJson = JSONObject.fromObject(statusMap.get("data"));
        return resultJson.toString();
    }


    /**
     * @Description  上传图片
     * @Author zhangxue
     * @Date 17:33 2018/5/12
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/uploadImage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String uploadImage(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String EquNo = request.getParameter("EquNo")!=null?request.getParameter("EquNo").toString():null;
        if (file.isEmpty()) {
            return "文件为空";
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        logger.info("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        logger.info("上传的后缀名为：" + suffixName);
        // 文件上传后的路径
        URL path = GoController.class.getResource("/static/images");
        String filePath = path.getPath()+"/"+EquNo+"/";
        // 解决中文问题，liunx下中文路径，图片显示问题
        // fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);

        // 检测是否存在目录
        String saveUrl = EquNo+"/"+fileName;
        //將图片路径存入数据库中
        Map<String,Object> imageParam = new HashMap<String,Object>();
        imageParam.put("ImageUrl",saveUrl);
        imageParam.put("EquNo",EquNo);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");

        //保存图片信息到后端数据库
        JSONObject jso = JSONObject.fromObject(imageParam);
        Map<String,Object> imgResMap = okHttpUtils.sendPostJson(BACKURL+"/saveEquImageInfos",headersParams,jso.toString(),new Object());
        String data = imgResMap.get("data")!=null?imgResMap.get("data").toString():null;
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败";
    }

    /**
     * @Description 得到所有图片的base64格式
     * @Author zhangxue
     * @Date 8:29 2018/5/14
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryImageUrlByEquNo",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryImageUrlByEquNo(@RequestBody Map<String,Object> map) {

        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");

        JSONObject jso = JSONObject.fromObject(map);
        Map<String,Object> imgResMap = okHttpUtils.sendPostJson(BACKURL+"/queryImageUrlByEquNo",headersParams,jso.toString(),new Object());
        String data = imgResMap.get("data")!=null?imgResMap.get("data").toString():null;
        Map<String,String> imgurlMap = (Map<String,String>)JSONObject.fromObject(data);
        URL path = GoController.class.getResource("/static/images");
        Map<String,Object> tempMap = new HashMap<String,Object>();
        for(int i=0;i<imgurlMap.size();i++){

            String filePath = path.getPath()+"/"+imgurlMap.get("ImageUrl"+(i+1));
            String baseStr = imageToBase.GetImageStr(filePath);
            tempMap.put("ImageUrl"+(i+1),baseStr);
        }
        JSONObject jsonObject = JSONObject.fromObject(tempMap);
        return jsonObject.toString();
    }
    /**
     * @Description  通过设备id查询设备维修历程信息
     * @Author zhangxue
     * @Date 15:17 2018/5/7
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryBasicEquCourseById",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryBasicEquCourseById(@RequestParam Map<String,Object> map) {
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        JSONObject jso = JSONObject.fromObject(map);
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryBasicEquCourseById",headersParams,jso.toString(),new Object());
        String data = resultMap.get("data")!=null?resultMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 通过id删除单条数据
     * @Author zhangxue
     * @Date 15:15 2018/5/12
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/delBasicEquById",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delBasicEquById(@RequestBody Map<String,Object> map) {
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        JSONObject jso = JSONObject.fromObject(map);
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/delBasicEquById",headersParams,jso.toString(),new Object());
        String data = resultMap.get("data")!=null?resultMap.get("data").toString():null;
        Map<String,Object> resMap = JSONObject.fromObject(data);
        String status = resMap.get("Status")!=null?resMap.get("Status").toString():"0";
        Map<String,Object> tempMap = new HashMap<String, Object>();
        if("1".equals(status)){
            URL path = GoController.class.getResource("/static/images");
            if(map.get("EquNo")!=null&!"".equals(map.get("EquNo").toString())){
                String filePath = path.getPath()+"/"+map.get("EquNo").toString();
                filePath = filePath.substring(1);
                Boolean flag = fileUtil.deleteDirectory(filePath);
                if(flag){
                    tempMap.put("Status","1");
                }
            }
        }
        return data;
    }


    /**
     * @Description 通过资产编号修改资产基本信息
     * @Author zhangxue
     * @Date 15:15 2018/5/12
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/updateBasicEquByEquNo",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateBasicEquByEquNo(@RequestBody Map<String,Object> map) {
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        JSONObject jso = JSONObject.fromObject(map);
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/updateBasicEquByEquNo",headersParams,jso.toString(),new Object());
        String data = resultMap.get("data")!=null?resultMap.get("data").toString():null;
        JSONObject resMap = JSONObject.fromObject(data);
        return resMap.toString();
    }


    /*####################################################################################*/
    /*##############################   维修管理    #######################################*/
    /*####################################################################################*/

    /**
     * @Description 查询所有维修单信息
     * @Author zhangxue
     * @Date 9:00 2018/5/8
     * @Param
     * @retrun
     **/
    @RequestMapping(value = "/queryAllConseDoc",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllConseDoc(@RequestParam Map<String,Object> map, HttpSession session) {
        JSONObject jsonObject = JSONObject.fromObject(map);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllConseDoc",headersParams,jsonObject.toString(),new Object());
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description  提交维修单
     * @Author zhangxue
     * @Date 17:09 2018/5/7
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/saveConseDoc",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveConseDoc(@RequestBody Map<String,Object> map) {
        JSONObject jsonObject = JSONObject.fromObject(map);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/saveConseDoc",headersParams,jsonObject.toString(),new Object());
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 通过id查询资产信息
     * @Author zhangxue
     * @Date 10:34 2018/5/8
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryBasicEquById",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryBasicEquById(@RequestBody Map<String,Object> map) {
        JSONObject jsonObject = JSONObject.fromObject(map);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/queryBasicEquById",headersParams,jsonObject.toString(),new Object());
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }


    /**
     * @Description 执行待执行维修单
     * @Author zhangxue
     * @Date 15:24 2018/5/8
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/saveConseDocAndConseCourse",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveConseDocAndConseCourse(@RequestBody Map<String,Object> map) {
        JSONObject jsonObject = JSONObject.fromObject(map);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/saveConseDocAndConseCourse",headersParams,jsonObject.toString(),new Object());
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description  刪除維修單
     * @Author zhangxue
     * @Date 11:19 2018/5/14
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/deleteConseveById",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteConseveById(@RequestBody Map<String,Object> map) {
        JSONObject jsonObject = JSONObject.fromObject(map);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/deleteConseveById",headersParams,jsonObject.toString(),new Object());
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }


    /*####################################################################################*/
    /*##############################   下拉框设置    #######################################*/
    /*####################################################################################*/
    @RequestMapping(value="/saveSettings",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveSettings(@RequestParam Map<String,Object> map,HttpServletRequest request) {
        String equtype = request.getParameter("equtype");
        String[] EquType = request.getParameterValues("EquType");
        String purchasetype = request.getParameter("purchasetype");
        String[] PurchaseType = request.getParameterValues("PurchaseType");
        String equstatus = request.getParameter("equstatus");
        String[] EquStatus = request.getParameterValues("EquStatus");
        String consetype = request.getParameter("consetype");
        String[] ConseType = request.getParameterValues("ConseType");
        Map<String,Object> paramMap = new HashMap<String, Object>();
        paramMap.put("equtype",equtype);
        paramMap.put("EquType",EquType);
        paramMap.put("purchasetype",purchasetype);
        paramMap.put("PurchaseType",PurchaseType);
        paramMap.put("equstatus",equstatus);
        paramMap.put("EquStatus",EquStatus);
        paramMap.put("consetype",consetype);
        paramMap.put("ConseType",ConseType);

        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");

        JSONObject jsonObject = JSONObject.fromObject(paramMap);
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/saveSettings",headersParams,jsonObject.toString(),new Object());
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 导出excel(资产基本信息)
     * @Author zhangxue
     * @Date 14:53 2018/6/14
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/CreateBasicExcel")
    public void CreateBasicExcel(@RequestParam Map<String,Object> param, HttpServletResponse response, ServletOutputStream out){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/CreateBasicExcel",headersParams,jsonObject.toString(),new Object());
        System.out.print(resultMap.toString());
        String tempStr = resultMap.get("data").toString();
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(tempStr);
        JSONArray ja = JSONArray.fromObject(js.get("data"));
        List list = new ArrayList();
        for(int i=0;i<ja.size();i++){
            AssessExcel assessExcel = new AssessExcel();
            assessExcel.setEquNo(jxJson("EquNo",ja.get(i).toString()));
            assessExcel.setStore(jxJson("Store",ja.get(i).toString()));
            assessExcel.setEquName(jxJson("EquName",ja.get(i).toString()));
            assessExcel.setEquType(jxJson("EquType",ja.get(i).toString()));
            assessExcel.setSpecification(jxJson("Specification",ja.get(i).toString()));
            assessExcel.setLifeTime(jxJson("LifeTime",ja.get(i).toString()));
            assessExcel.setBuyInTime(jxJson("BuyInTime",ja.get(i).toString()));
            assessExcel.setStartTime(jxJson("StartTime",ja.get(i).toString()));
            assessExcel.setEndTime(jxJson("EndTime",ja.get(i).toString()));
            assessExcel.setBuyInMoney(jxJson("BuyInMoney",ja.get(i).toString()));
            assessExcel.setServCondition(jxJson("ServCondition",ja.get(i).toString()));
            assessExcel.setPurchaseType(jxJson("PurchaseType",ja.get(i).toString()));
            assessExcel.setEquStatus(jxJson("EquStatus",ja.get(i).toString()));
            assessExcel.setConsePeriod(jxJson("ConsePeriod",ja.get(i).toString()));
            assessExcel.setPlace(jxJson("Place",ja.get(i).toString()));
            assessExcel.setInputMan(jxJson("InputMan",ja.get(i).toString()));
            list.add(assessExcel);
        }
        // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("后勤资产信息");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("设备编号");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("中心店面");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("资产名称");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("类别");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("型号-规格");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("寿命");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("购入日期");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("开始使用时间");
        cell.setCellStyle(style);
        cell = row.createCell((short) 8);
        cell.setCellValue("截止使用时间");
        cell.setCellStyle(style);
        cell = row.createCell((short) 9);
        cell.setCellValue("购买费用(元)");
        cell.setCellStyle(style);
        cell = row.createCell((short) 10);
        cell.setCellValue("使用状况");
        cell.setCellStyle(style);
        cell = row.createCell((short) 11);
        cell.setCellValue("购置方式");
        cell.setCellStyle(style);
        cell = row.createCell((short) 12);
        cell.setCellValue("设备状态");
        cell.setCellStyle(style);
        cell = row.createCell((short) 13);
        cell.setCellValue("维护周期");
        cell.setCellStyle(style);
        cell = row.createCell((short) 14);
        cell.setCellValue("存放地点");
        cell.setCellStyle(style);
        cell = row.createCell((short) 15);
        cell.setCellValue("录入人");
        cell.setCellStyle(style);


        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，

        for (int i = 0; i < list.size(); i++)
        {
            row = sheet.createRow((int) i + 1);
            AssessExcel assessExcel = (AssessExcel) list.get(i);
            // 第四步，创建单元格，并设置值
            row.createCell((short) 0).setCellValue(assessExcel.getEquNo());
            row.createCell((short) 1).setCellValue(assessExcel.getStore());
            row.createCell((short) 2).setCellValue(assessExcel.getEquName());
            row.createCell((short) 3).setCellValue(assessExcel.getEquType());
            row.createCell((short) 4).setCellValue(assessExcel.getSpecification());
            row.createCell((short) 5).setCellValue(assessExcel.getLifeTime());

            cell = row.createCell((short) 6);
            String BuyInTimeStr = formatData("yyyy-MM-dd",Long.valueOf(assessExcel.getBuyInTime()));
            cell.setCellValue(BuyInTimeStr);

            cell = row.createCell((short) 7);
            String StartTimeStr = formatData("yyyy-MM-dd",Long.valueOf(assessExcel.getStartTime()));
            cell.setCellValue(StartTimeStr);

            cell = row.createCell((short) 8);
            String EndTimeStr = formatData("yyyy-MM-dd",Long.valueOf(assessExcel.getEndTime()));
            cell.setCellValue(EndTimeStr);

            row.createCell((short) 9).setCellValue(assessExcel.getBuyInMoney());
            row.createCell((short) 10).setCellValue(assessExcel.getServCondition());
            row.createCell((short) 11).setCellValue(assessExcel.getPurchaseType());
            row.createCell((short) 12).setCellValue(assessExcel.getEquStatus());
            row.createCell((short) 13).setCellValue(assessExcel.getConsePeriod());
            row.createCell((short) 14).setCellValue(assessExcel.getPlace());
            row.createCell((short) 15).setCellValue(assessExcel.getInputMan());
            logger.info("*******************"+assessExcel.toString()+"*****************");
            System.out.print(assessExcel.toString());
        }
        // 第六步，将文件存到指定位置
        try {
            response.setContentType("application/vnd.ms-excel");
            wb.write(out);

            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @Description 导出excel2(维护管理信息)
     * @Author zhangxue
     * @Date 14:53 2018/6/14
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/CreateConseExcel")
    public void CreateConseExcel(@RequestParam Map<String,Object> param, HttpServletResponse response, ServletOutputStream out){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/CreateConseExcel",headersParams,jsonObject.toString(),new Object());
        String tempStr = resultMap.get("data").toString();
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(tempStr);
        JSONArray ja = JSONArray.fromObject(js.get("data"));
        List list = new ArrayList();
        for(int i=0;i<ja.size();i++){
            AssessExcel2 assessExcel = new AssessExcel2();
            assessExcel.setDocNo(jxJson("DocNo",ja.get(i).toString()));
            assessExcel.setStore(jxJson("Store",ja.get(i).toString()));
            assessExcel.setEquName(jxJson("EquName",ja.get(i).toString()));
            assessExcel.setEquType(jxJson("EquType",ja.get(i).toString()));
            assessExcel.setDocStatus(jxJson("DocStatus",ja.get(i).toString()));
            assessExcel.setCreateTime(jxJson("CreateTime",ja.get(i).toString()));
            assessExcel.setConseTime(jxJson("ConseTime",ja.get(i).toString()));
            assessExcel.setConseType(jxJson("ConseType",ja.get(i).toString()));
            assessExcel.setConseResult(jxJson("ConseResult",ja.get(i).toString()));
            assessExcel.setConseReason(jxJson("ConseReason",ja.get(i).toString()));
            assessExcel.setCost(jxJson("Cost",ja.get(i).toString()));
            assessExcel.setInputMan(jxJson("InputMan",ja.get(i).toString()));
            list.add(assessExcel);
        }
        // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("后勤维护管理信息");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("作业编号");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("中心店面");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("资产名称");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("类别");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("作业状态");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("创建时间");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("完成时间");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("维护类型");
        cell.setCellStyle(style);
        cell = row.createCell((short) 8);
        cell.setCellValue("维护原因");
        cell.setCellStyle(style);
        cell = row.createCell((short) 9);
        cell.setCellValue("审批人(文件)");
        cell.setCellStyle(style);
        cell = row.createCell((short) 10);
        cell.setCellValue("花费");
        cell.setCellStyle(style);
        cell = row.createCell((short) 11);
        cell.setCellValue("录入人");
        cell.setCellStyle(style);


        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，

        for (int i = 0; i < list.size(); i++)
        {
            row = sheet.createRow((int) i + 1);
            AssessExcel2 assessExcel = (AssessExcel2) list.get(i);
            // 第四步，创建单元格，并设置值
            row.createCell((short) 0).setCellValue(assessExcel.getDocNo());
            row.createCell((short) 1).setCellValue(assessExcel.getStore());
            row.createCell((short) 2).setCellValue(assessExcel.getEquName());
            row.createCell((short) 3).setCellValue(assessExcel.getEquType());

            String tempDocStatus = assessExcel.getDocStatus()!=null?assessExcel.getDocStatus().toString():null;
            if("2".equals(tempDocStatus)){
                row.createCell((short) 4).setCellValue("执行中");
            }else{
                row.createCell((short) 4).setCellValue("执行完毕");
            }
            cell = row.createCell((short) 5);
            String CreateTimeStr = formatData("yyyy-MM-dd",Long.valueOf(assessExcel.getCreateTime()));
            cell.setCellValue(CreateTimeStr);

            if(assessExcel.getConseTime()!=null&&!"".equals(assessExcel.getConseTime())){
                cell = row.createCell((short) 6);
                String ConseTimeStr = formatData("yyyy-MM-dd",Long.valueOf(assessExcel.getConseTime()));
                cell.setCellValue(ConseTimeStr);
            }else{
                cell = row.createCell((short) 6);
                cell.setCellValue("");
            }
            String tempConseType = assessExcel.getConseType()!=null?assessExcel.getConseType().toString():null;
            if("1".equals(tempConseType)){
                row.createCell((short) 7).setCellValue("定期维保");
            }else if("2".equals(tempConseType)){
                row.createCell((short) 7).setCellValue("更换备件");
            }else{
                row.createCell((short) 7).setCellValue("报废更换");
            }
            row.createCell((short) 8).setCellValue(assessExcel.getConseReason());
            row.createCell((short) 9).setCellValue(assessExcel.getConseResult());
            row.createCell((short) 10).setCellValue(assessExcel.getCost());
            row.createCell((short) 11).setCellValue(assessExcel.getInputMan());

        }
        // 第六步，将文件存到指定位置
        try {
            response.setContentType("application/vnd.ms-excel");
            wb.write(out);
            out.flush();
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public String jxJson(String mkey, String strJson) {
        String value = "";
        try {
            JSONObject json = JSONObject.fromObject(strJson);
            Iterator iterator = json.keys();
            while (iterator.hasNext()) {
                String key = iterator.next() + "";

                if (json.getString(key).startsWith("{")) {
                    value = jxJson(mkey, json.getString(key));
                    break;
                } else {
                    if (key.equals(mkey)) {
                        value = json.getString(key);
                        break;
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (value.equals("null")) {
            value = "";
        }
        return value;
    }

    /**
     * 时间戳转换成日期格式字符串
     * @param
     * @param
     * @return
     */
    public static String formatData(String dataFormat, long timeStamp) {
        if (timeStamp == 0) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat(dataFormat);
        return format.format(new Date(timeStamp));
    }

}
